using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Advanced_Terrain
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class WindArrow : DrawableGameComponent
    {
        ContentManager _content;
        Model _model;
        Matrix[] _boneTransforms;
        public Vector3 Position = new Vector3(10,10,0);
        float Scale = .015f;
        public Quaternion Rotation = Quaternion.Identity;
        private Texture2D _Texture;
        public GraphicsDevice graphics;
        public Boolean IsVisible = true;
        public Vector3 CameraPosition = new Vector3();
        public Matrix CameraRotation = new Matrix();
        public float WindPower = 1;
        public WindArrow(Game game, ContentManager content)
            : base(game)
        {
            _content = content;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }
        protected override void LoadContent()
        {
            _model = _content.Load<Model>("WindArrow");
            // Allocate the transform matrix array.
            _boneTransforms = new Matrix[_model.Bones.Count];
            _Texture = _content.Load<Texture2D>("arrowtexture");
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Position = CameraPosition;
            Matrix Temp = Matrix.CreateLookAt(CameraPosition,CameraPosition+Game1.newTerrain.windDirection,Vector3.Up);
            Rotation = Quaternion.CreateFromRotationMatrix(Temp);

            
            Position += Vector3.Transform(new Vector3(.53f,.36f,-1.25f), CameraRotation);
            //Position.Z -= 2f; 
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            
            if (IsVisible)
            {
                _model.CopyAbsoluteBoneTransformsTo(_boneTransforms);
                // Draw the model.
                foreach (ModelMesh mesh in _model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.Begin();
                        effect.Projection = CameraProperties.projectionMatrix;
                        effect.World = _boneTransforms[mesh.ParentBone.Index] * Matrix.CreateScale(Scale) * Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position);
                        //effect.World = effect.World * Matrix.CreateFromQuaternion(Rotation) *Matrix.Identity;
                        effect.View = CameraProperties.View;
                        effect.EnableDefaultLighting();
                        //effect.TextureEnabled = true;
                        //effect.Texture = _Texture;
                        effect.End();
                    }

                    mesh.Draw();

                }
            }

            base.Draw(gameTime);
        }
    }
}